using B.S.RbacData.Infrastructure;
using ContentmanagementBase.Infrastructure;
using ContentmanagementBaseWrite.API.AutomapperFiles;
using ContentmanagementBaseWrite.API.Services;
using Microsoft.EntityFrameworkCore;
using NLog.Web;
using System.Reflection;

namespace ContentmanagementBaseWrite.API
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            // Add services to the container.
            //添加数据库连接
            builder.Services.AddDbContext<ContentManagementDBContext>(x =>
            {
                x.UseMySql(builder.Configuration.GetConnectionString("mysql"),new MySqlServerVersion("5.7"));
            });
            //注册Nlog
            builder.Host.UseNLog();
            //注册中介者模式
            builder.Services.AddMediatR(x => x.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()));
            //注册仓储
            builder.Services.AddScoped(typeof(IBaseRepository<>),typeof(BaseRepository<>));
            // 注册自动映射
            builder.Services.AddAutoMapper(typeof(AutomapperProFiles));
            // 注册Minio服务
            builder.Services.AddScoped<IMinioService, MinioService>();

            builder.Services.AddControllers();
            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            builder.Services.AddEndpointsApiExplorer();
            //Swaggerע
            builder.Services.AddSwaggerGen(o =>
            {
                //ȡĿ
                var projectName = typeof(Program).Assembly.GetName().Name;

                var path = AppDomain.CurrentDomain.BaseDirectory + projectName + ".xml";

                o.IncludeXmlComments(path, true);
            });


            var app = builder.Build();

            //跨域
            app.UseCors(x=>x.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin());


            // Configure the HTTP request pipeline.
            if (app.Environment.IsDevelopment())
            {
                app.UseSwagger();
                app.UseSwaggerUI();
            }

            app.UseAuthorization();


            app.MapControllers();

            app.Run();
        }
    }
}
